﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.BLL.Extend;
using ZX.SYS.Library;
using ZX.EHR.Library;
using ZX.Common;
using ZX.Common.Extend;
using ZX.DBModule;

namespace ZX.BLL
{
    public class BEmployeePositions : BaseBussnies
    {
        public override bool Insert(object entitys, out string msg)
        {
            bool rs = base.Insert(entitys, out msg);
            if (rs)
            {
                RunPositionJob();
            }
            return rs;
        }

        public override bool Update(object entitys, out string msg)
        {
            bool rs = base.Update(entitys, out msg);
            if (rs)
            {
                RunPositionJob();
            }
            return rs;
        }

        private void RunPositionJob()
        {
            string condition = string.Empty;
            BaseEntity instance = null;
            ZX.BLL.Extend.BaseBussnies bll = null;
            IList<BaseEntity> list = null;
            BaseEntity hrEmployeeInstance = ZX.Common.Globals.getEntity("hrEmployee") as BaseEntity;
            ZX.BLL.Extend.BaseBussnies hrEmployeeBll = new ZX.BLL.Extend.BaseBussnies(hrEmployeeInstance);
            try
            {
                //岗位异动后要更新员工信息表中的部门，职务信息
                //获取岗位异动表中当前日期>=异动日期的记录
                condition = string.Format("ValidDate<='{0}'and AvilableStatus=3 and (PostType=0 or PostType=2)", DateTime.Now.ToString("yyyy-MM-dd"));
                instance = ZX.Common.Globals.getEntity("hrEmployeePositions") as BaseEntity;
                bll = new ZX.BLL.Extend.BaseBussnies(instance);

                list = bll.GetEntitysByCondition(condition);//获取所有符合条件的记录
                if (list != null)
                {
                    foreach (BaseEntity entity in list)
                    {
                        hrEmployeePositions hremployeePositions = entity as hrEmployeePositions;
                        condition = string.Format("EmployeeID='{0}' and AvilableStatus=2 and (PostType=0 or PostType=2)", hremployeePositions.EmployeeID.ToString());
                        IList<BaseEntity> list2 = bll.GetEntitysByCondition(condition);//获取目前正在就职的岗位
                        if (list2 != null)
                        {
                            for (int i = 0; i < list2.Count; i++)
                            {
                                hrEmployeePositions tempHrEmployeePositions = list2[i] as hrEmployeePositions;
                                tempHrEmployeePositions.AvilableStatus = 1;//将之前正在就职的岗位状态更新为过期
                                bll = new ZX.BLL.Extend.BaseBussnies(tempHrEmployeePositions);
                                bll.Update();
                            }
                        }
                        hremployeePositions.AvilableStatus = 2;//更新岗位状态为正在就职
                        bll = new ZX.BLL.Extend.BaseBussnies(hremployeePositions);
                        bll.Update();
                        hrEmployee hremployee = hrEmployeeBll.GetEntityById(hremployeePositions.EmployeeID.ToString()) as hrEmployee;
                        hremployee.PostID = hremployeePositions.PostID;//更新岗位   
                        hremployee.DepartmentID = hremployeePositions.DeptID; //更新员工部门
                        hremployee.PositionLevel = hremployeePositions.PostRank;//更新职等
                        if (hremployeePositions.PostType == 2)
                        {
                            hremployee.EmployeeStatus = 1;//返聘后状态改为在职
                        }
                        hrEmployeeBll.Update(hremployee);//更新到数据库}
                    }
                }
            }
            catch (Exception e)
            { }
        }
    }
}
